package com.rrd.mex.dw.mr.babel.parser;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.tanzhi.MultiPlatformReportRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;

/**
 * @Classname MultiPlatformReportRecordParse
 * @Description 探知-多头报告
 * @Date 2023/8/9 18:55
 * @Created by zxb
 */

public class MultiPlatformReportRecordParse extends ParseBase {
    Text key = new Text();
    Text value = new Text();

    @Override
    public boolean parseJob(Mapper.Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        MultiPlatformReportRecord obj = (MultiPlatformReportRecord) model;
        StringBuilder sb = getBaseInfo(obj, successful,1);

        sb.append(obj.getCode() == null ? "" : obj.getCode()).append(CRT001);
        sb.append(obj.getMsg() == null ? "" : obj.getMsg()).append(CRT001);
        sb.append(obj.getToken() == null ? "" : obj.getToken()).append(CRT001);

        JSONObject data = obj.getData() == null ? new JSONObject() : obj.getData();
        sb.append(formatValue(data.getString("tzbg_loaprelati_m24"))).append(CRT001);
        sb.append(formatValue(data.getString("tzbg_lorelati_m24"))).append(CRT001);
        sb.append(formatValue(data.getString("tzbg_loticalati_m24"))).append(CRT001);
        sb.append(formatValue(data.getString("tzbg_looflati_m24"))).append(CRT001);
        sb.append(formatValue(data.getString("tzbg_loreevcogr_m24"))).append(CRT001);
        sb.append(formatValue(data.getString("tzbg_lorerelati_m24"))).append(CRT001);
        sb.append(formatValue(data.getString("tzbg_loovlati_m24"))).append(CRT001);

        JSONArray staticData = data.getJSONArray("staticData") == null ? new JSONArray() : data.getJSONArray("staticData");
        String[] str = {"w1","w2","w3","m1","m2","m3","m6","m9","m12","m15","m18","m24"};
        int i = 0;
        while(i < str.length) {
            int sum = 0;
            for (int j = 0; j < staticData.size(); j++) {
                if (str[i].equals(staticData.getJSONObject(j).getString("slice_name"))){
                    sb.append(staticData.getJSONObject(j).getString("tzbg_loadev")).append(CRT001);
                    sb.append(staticData.getJSONObject(j).getString("tzbg_loadpl")).append(CRT001);
                    sb.append(staticData.getJSONObject(j).getString("tzbg_lovecoev")).append(CRT001);
                    sb.append(staticData.getJSONObject(j).getString("tzbg_lovecopl")).append(CRT001);
                    sb.append(staticData.getJSONObject(j).getString("tzbg_loreev")).append(CRT001);
                    sb.append(staticData.getJSONObject(j).getString("tzbg_lorepl")).append(CRT001);
                    sb.append(staticData.getJSONObject(j).getString("tzbg_loapreev")).append(CRT001);
                    sb.append(staticData.getJSONObject(j).getString("tzbg_loaprepl")).append(CRT001);
                    sb.append(staticData.getJSONObject(j).getString("tzbg_loapjeev")).append(CRT001);
                    sb.append(staticData.getJSONObject(j).getString("tzbg_loapjepl")).append(CRT001);
                    sb.append(staticData.getJSONObject(j).getString("tzbg_loticaev")).append(CRT001);
                    sb.append(staticData.getJSONObject(j).getString("tzbg_looffev")).append(CRT001);
                    sb.append(staticData.getJSONObject(j).getString("tzbg_looffpl")).append(CRT001);
                    sb.append(staticData.getJSONObject(j).getString("tzbg_lorereev")).append(CRT001);
                    sb.append(staticData.getJSONObject(j).getString("tzbg_lorerepl")).append(CRT001);
                    sb.append(staticData.getJSONObject(j).getString("tzbg_loresuev")).append(CRT001);
                    sb.append(staticData.getJSONObject(j).getString("tzbg_loovev")).append(CRT001);
                    sb.append(staticData.getJSONObject(j).getString("tzbg_loovpl")).append(CRT001);
                    sb.append(staticData.getJSONObject(j).getString("tzbg_loseovev")).append(CRT001);
                    sum = sum + 1;
                    break;
                }else {
                    continue;
                }
            }

            if (sum >= 1) {
               // do nothing
            }else {
                sb.append("").append(CRT001);
                sb.append("").append(CRT001);
                sb.append("").append(CRT001);
                sb.append("").append(CRT001);
                sb.append("").append(CRT001);
                sb.append("").append(CRT001);
                sb.append("").append(CRT001);
                sb.append("").append(CRT001);
                sb.append("").append(CRT001);
                sb.append("").append(CRT001);
                sb.append("").append(CRT001);
                sb.append("").append(CRT001);
                sb.append("").append(CRT001);
                sb.append("").append(CRT001);
                sb.append("").append(CRT001);
                sb.append("").append(CRT001);
                sb.append("").append(CRT001);
                sb.append("").append(CRT001);
                sb.append("").append(CRT001);
            }

            i++;
        }

        key.set(getKey());
        value.set(sb.toString().replace("\n", " ").replace("\r", " "));
        context.write(key, value);
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_tanzhi_multi_platform_report_inc";
    }

    @Override
    public String getType() {
        return RecordType.MULTI_PLATFORM_REPORT_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "探知-多头报告";
    }

    @Override
    public BaseModel getModel() {
        return new MultiPlatformReportRecord();
    }
}
